using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

using SBPweb.Modules.Modularity;
using SBPweb.Modules.Smartparts;

namespace SBPweb.Presentation.Windows.Forms.Controls
{
    public partial class ModalDockingUserControl : DockingUserControl
    {
        #region Private members

        private bool mIsControlBoxVisible = false;
        private bool mIsBorderVisible = true;
        private bool mIsFullscreen = false;

        #endregion

        #region Constructor logic

        /// <summary>
        /// Modal UserControl of the module
        /// </summary>
        /// <param name="parentModule">Parent module </param>
        public ModalDockingUserControl(IModule parentModule)
            : base(parentModule)
        {
            IsModal = true;
            InitialAppearanceStyle = AppearanceStyle.Float;
            AllowClose = true;
            AllowDockBottom = false;
            AllowDockCenter = false;
            AllowDockLeft = false;
            AllowDockRight = false;
            AllowDockTop = false;
            AllowDrop = false;
            AllowFloat = true;
            Collapsed = false;
            HideOnlyWhenClose = false;
            Caption = parentModule.Name;
        }

        protected override void DockControl_DockSituationChanged(object sender, EventArgs e)
        {
            if (sender != DockControl) return;

            base.DockControl_DockSituationChanged(sender, e);

            ModalDockingUserControl mduc = null;
            if (DockControl.Controls != null)
            {
                foreach (Control c in DockControl.Controls)
                {
                    mduc = c as ModalDockingUserControl;
                }
            }

            if (mduc != null)
            {
                //
                // for modal: set topmost
                //
                if (DockControl != null && DockControl.Parent != null && DockControl.Parent.Parent != null)
                {
                    Form f = DockControl.Parent.Parent as Form;
                    if (f != null)
                    {
                        f.TopMost = true;

                        //f.FormBorderStyle = FormBorderStyle.FixedToolWindow;

                        if (!mduc.IsBorderVisible)
                        {
                            f.FormBorderStyle = FormBorderStyle.None;
                        }
                        if (!mduc.IsControlBoxVisible)
                        {
                            f.ControlBox = false;
                        }
                        else
                        {
                            f.ControlBox = true;
                        }
                        if (mduc.IsFullscreen && f.Visible == false)
                        {
                            f.Load -= new EventHandler(LLoad);
                            f.Load += new EventHandler(LLoad);
                        }
                    }
                }
            }
        }

        protected void LLoad(object sender, EventArgs e)
        {
            (sender as Form).WindowState = FormWindowState.Maximized;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the value indicatig whether the ISmartpart has ControlBox.
        /// </summary>
        public bool IsControlBoxVisible
        {
            get
            {
                return mIsControlBoxVisible;
            }
            set
            {
                mIsControlBoxVisible = value;
            }
        }

        /// <summary>
        /// Gets or sets the value indicatig whether the border of ISmartpart is visible.
        /// </summary>
        public bool IsBorderVisible
        {
            get
            {
                return mIsBorderVisible;
            }
            set
            {
                mIsBorderVisible = value;
            }
        }

        /// <summary>
        /// Gets or sets the value indicatig whether the ISmartpart is displayed on FullScreen.
        /// </summary>
        public bool IsFullscreen
        {
            get
            {
                return mIsFullscreen;
            }
            set
            {
                mIsFullscreen = value;
            }
        }

        #endregion

        #region internal methods

        [EditorBrowsable(EditorBrowsableState.Never)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Bindable(false)]
        internal void FireDisplayFailedEvent()
        {
            if (DisplayFailed != null)
            {
                DisplayFailed(this, null);
            }
        }

        #endregion

        #region Public events

        /// <summary>
        /// Occurs, when the parent module already has a visible ModalDockingUserControl
        /// </summary>
        public event EventHandler DisplayFailed;

        #endregion

    }

}
